
	initEditor()

	const form = layui.form
	$('.head ul').on('click', 'li', function () {
		$(this).addClass('active').siblings().removeClass('active')
		$('.spit').css('left', $(this).offset().left - 30)
		$('.box').eq($(this).index()).show().siblings('.box').hide()
	})


	layui.use('laydate', function () {
		var laydate = layui.laydate;
		//执行一个laydate实例
		laydate.render({
			elem: '#Date' //指定元素
		});
	});

	$.ajax({
		type: "GET",
		url: "/ydd_cate/all",
		success: ({ errmsg, errno, data }) => {
			if (errno) return layer.msg(errmsg, { icon: 5, time: 1000 })
			$('[name="cateid"]').append(data.map(({ catename, id }) => catename ? `
      <option  value="${id}" >${catename}</option>` : '').join(''))
			form.render()
		}
	})

	const articleId = localStorage.getItem('articleId')
	$.ajax({
		type: "GET",
		url: "/ydd_tag/all",
		success: ({ errmsg, errno, data }) => {
			console.log(36);
			if (errno) return layer.msg(errmsg, { icon: 5, time: 1000 })
			$('.checkbox').html(data.map(({ tagname, id }) => `
      <input type="checkbox" data-id=${id}  title="${tagname}">`).join(''))
			form.render()
			console.log(articleId);
			if (articleId) {
				$.ajax({
					type: "GET",
					url: "/ydd_article/rowInfo",
					data: {
						id: articleId
					},
					success: ({ errmsg, errno, data }) => {
						console.log(1);
						const { cateid, pic, tags, create_date, content } = data
						if (errno) return layer.msg(errmsg, { icon: 5, time: 1000 })
						form.val('search', data)
						form.render()

						$('[name=create_date]').val(new Date(create_date).toLocaleDateString().replace(/\//g, '-'))
						$('.layui-anim-upbit dd').each(function () {
							if (+$(this).attr('lay-value') === cateid) $(this).addClass('layui-this')
						})
						$('.layui-form-checkbox').each(function (index, item) {
							const key = $(item).find('span').html()
							if (tags.split(',').some(val => val === key)) {
								$(item).addClass('layui-form-checked')
							}
						})

						tinyMCE.activeEditor.setContent(content)
						$('#img').prop('src', `http://124.223.14.236:8060/${pic}`).parents('.img').show()


					}
				});
			}
		}
	})


	$('#upload').on('click', () => $('#file').click())

	let file = null
	$('#file').on('change', e => {
		file = e.target.files
		const imgUrl = URL.createObjectURL(file[0])
		$('#img').prop('src', imgUrl).parents('.img').show()
	})


	$('.sub').on('click', () => {
		let num = $('[name=click]').val()
		if (num <= 0) return $('[name=click]').prop('disabled', true)
		$('[name=click]').val(--num)
	})

	$('.add').on('click', () => {
		let num = $('[name=click]').val()
		if (num >= 0) $('[name=click]').prop('disabled', false)
		$('[name=click]').val(++num)
	})



	function updata(data) {
		$.ajax({
			url: "/ydd_article/update",
			method: 'post',
			data: JSON.stringify(data),
			contentType: 'application/json',
			success: function ({ errmsg, errno }) {
				if (errno) return layer.msg(errmsg, { icon: 5, time: 1000 })
				$('.search')[0].reset()
				if (articleId) localStorage.removeItem('articleId')
				location.href = './article.html'
			}
		})
	}
	let status = 2

	function getData() {
		const data = {}
		const tags = []
		$('.layui-form-checked').each(function () {
			const key = $(this).find('span').html()
			tags.push(key)
		})
		const chinese = /[^\u4E00-\u9FA5]/g;
		$('.search').serialize().split('&')
			.map(item => item.split('='))
			.forEach(([one, two]) => data[one] = chinese.test(two) ? decodeURI(two) : two)
		data.tags = tags.join(',')
		data.create_date = +new Date(data.create_date)
		data.content = tinyMCE.activeEditor.getContent()
		data.ishot = data.ishot === 'on' ? 1 : 0
		data.istop = data.istop === 'on' ? 1 : 0
		data.click = +data.click
		data.cateid = +data.cateid
		data.status = status
		// data.user_id = +data.user_id
		data.updata_date = +new Date()
		data.id = +articleId
		console.log(data);
		return data
	}




	$('.search').on('submit', e => {
		e.preventDefault()
		const data = getData()
		if (file) {
			const pic = new FormData()
			pic.append('file', file[0])
			$.ajax({
				method: "post",
				url: "/upload?type=images",
				processData: false,
				data: pic,
				contentType: false,
				success: ({ errmsg, errno, data: { savePath } }) => {
					if (errno) return layer.msg(errmsg, { icon: 5, time: 1000 })
					data.pic = savePath
					updata(data)
				}
			})
		} else {
			data.pic = $('#img').prop('src').substring(27)
			updata(data)
		}
	})

	$('#save').on('click', () => status = 1)


	$('#back').on('click', () => {
		layer.confirm('确认返回列表吗?', function (index) {
			$('.search')[0].reset()
			localStorage.removeItem('articleId')
			layer.close(index)
			location.href = './article.html'
		});
	})

